package com.lz.comm.image.progress

import android.util.Log
import com.bumptech.glide.integration.okhttp3.OkHttpStreamFetcher
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.load.model.MultiModelLoaderFactory
import okhttp3.Call
import java.io.InputStream

class ProgressOkHttpUrlLoader(private val client: Call.Factory) : ModelLoader<ProgressGlideUrl, InputStream> {

    override fun buildLoadData(
        model: ProgressGlideUrl,
        width: Int,
        height: Int,
        options: Options
    ): ModelLoader.LoadData<InputStream> {
        Log.i("youxiaochen", "buildLoadData...")
        return ModelLoader.LoadData(model, OkHttpStreamFetcher(client, model))
    }

    override fun handles(model: ProgressGlideUrl): Boolean = true


    class Factory(private val client: Call.Factory) : ModelLoaderFactory<ProgressGlideUrl, InputStream> {

        override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<ProgressGlideUrl, InputStream> {
            return ProgressOkHttpUrlLoader(client)
        }

        override fun teardown() {
            //nothing...
        }
    }

}